package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.MovieAdapter;
import flc.ast.databinding.ActivitySearchMovieBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.bean.StkResBean;
import stark.common.bean.StkResSetBean;

public class SearchMovieActivity extends BaseAc<ActivitySearchMovieBinding> {

    public static String movieKey;
    private List<StkResBean> listShow=new ArrayList<>();
    private MovieAdapter movieAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_search_movie;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSearchMovieBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivMovieSearch.setOnClickListener(this);
        mDataBinding.etMovieKey.setText(movieKey);
        mDataBinding.rvMovieList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        movieAdapter=new MovieAdapter();
        movieAdapter.setType(1);
        mDataBinding.rvMovieList.setAdapter(movieAdapter);
        movieAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivMovieSearch:
                movieKey=mDataBinding.etMovieKey.getText().toString().trim();
                getData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData(){
        listShow.clear();
        StkResApi.getTagResourceSetsList(null, AppConfig.SEARCH_DATA, movieKey, null, false, new IReqRetCallback<StkResSetBean>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable StkResSetBean result) {
                if (success){
                    if (result.articleList.size()>0){
                        listShow.addAll(result.articleList);
                    }
                }
                if (listShow.size()>0){
                    movieAdapter.setList(listShow);
                    mDataBinding.rvMovieList.setVisibility(View.VISIBLE);
                    mDataBinding.llSearchMovieNoData.setVisibility(View.GONE);
                }else {
                    mDataBinding.rvMovieList.setVisibility(View.GONE);
                    mDataBinding.llSearchMovieNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}